<?

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  // Get our other classes in one go here
  require('../incs/smarty/Smarty.class.php');
  require('../incs/fckeditor/fckeditor.php');
  require('../incs/sanitizept.inc.php');

    // Get our Config Settings
    $query_config = "SELECT * from orbitfaq_config";
    $result_config = $faqsql_query ($query_config)OR DIE( "$sql_query_error $query_config");;

    while($row_config = $faqsql_fetch_array($result_config))
      {

        $config_variable = $row_config[2];
        $config_value = $row_config[3];

        // Set our variables
        $$config_variable = $config_value;

      }

    $SkinFullPath = $absolute_path . $SkinPath;

  function CheckAdminAccess($faq, $ca, $qn, $accesslvl, $userid)
    {

      require('../incs/config.inc.php');
      require('../incs/db.inc.php');

      if($faq != '')
        {

          // Check if the admin has access
          $query_faqacl = "SELECT * from orbitfaq_admins_ownership WHERE f_id = '$faq' AND `admin_id` = '$userid'";
          $result_faqacl = $faqsql_query ($query_faqacl)OR DIE( "$sql_query_error $query_faqacl");
          $count_faqacl = $faqsql_count_rows($result_faqacl);

          if($count_faqacl != '0')
            {
              $superadminaccess = '1';
            }
          else
            {
              $superadminaccess = '0';
            }

        }
      else if(($faq == '')AND($ca != ''))
        {
            $query_faq = "SELECT f_id from orbitfaq_categories WHERE id = $ca";
            $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

            while ($row_faq = $faqsql_fetch_array ($result_faq)){
              $faq_id = $row_faq[0];

                // Check if the admin has access
                $query_faqacl = "SELECT * from orbitfaq_admins_ownership WHERE f_id = '$faq_id' AND `admin_id` = '$userid'";
                $result_faqacl = $faqsql_query ($query_faqacl)OR DIE( "$sql_query_error $query_faqacl");
                $count_faqacl = $faqsql_count_rows($result_faqacl);

                if($count_faqacl != '0')
                  {
                    $superadminaccess = '1';
                  }
                else
                  {
                    $superadminaccess = '0';
                  }

              }
        }
      else if(($faq == '')AND($ca == '')AND($qn != ''))
        {
            $query_faq = "SELECT f_id from orbitfaq_questions WHERE id = $qn";
            $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

            while ($row_faq = $faqsql_fetch_array ($result_faq)){
              $faq_id = $row_faq[0];

                // Check if the admin has access
                $query_faqacl = "SELECT * from orbitfaq_admins_ownership WHERE f_id = '$faq_id' AND `admin_id` = '$userid'";
                $result_faqacl = $faqsql_query ($query_faqacl)OR DIE( "$sql_query_error $query_faqacl");
                $count_faqacl = $faqsql_count_rows($result_faqacl);

                if(($faq_id == '0')OR($count_faqacl != '0'))
                  {
                    $superadminaccess = '1';
                  }
                else
                  {
                    $superadminaccess = '0';
                  }

              }
        }
      else
        {
          $superadminaccess = '0';
        }

      return $superadminaccess;
    }

    // Get our current version
      $query_package = "SELECT * from orbitfaq_ovc WHERE `package` = 'orbitfaq' ORDER BY major DESC, minor DESC, servicepack DESC, beta DESC LIMIT 1";
      $result_package = $faqsql_query ($query_package)OR DIE( "$sql_query_error $query_login");;

      while($row_package = $faqsql_fetch_array($result_package))
        {
          $v_id = $row_package[0];
          $v_package = $row_package[1];
          $v_major = $row_package[2];
          $v_minor = $row_package[3];
          $v_servicepack = $row_package[4];
          $v_beta = $row_package[5];

          if($v_beta != '0')
            {
              $v_beta = " Beta $v_beta";
            }
          else
            {
              $v_beta = '';
            }

          $orbitfaq_version = $v_major . "." . $v_minor . "." . $v_servicepack . "" . $v_beta;

        }

  // REQUIRED SWIFT EMAIL CLASSED

  // Swift comes in two version, for PHP4 & PHP5.
  // We must work out which one to include for this server.

    // Get our version
    $phpv = phpversion();

    // Strip to the first character
    $phpv = substr($phpv,0,1);

    // Lets check the version and show an error if it does not fit our requirements
    if(($phpv != '4')AND($phpv != '5')AND($forceLoad != '1'))
      {
        echo "You do not have an up to date version of PHP, you are currently running $phpv+.<br />Please update to version 4 or 5 at <a href='http://ww.php.net'>www.php.net</a><br /><br />";
        echo "If you wish to force the loading of OrbitFAQ without swift email functionalities please set the forceLoad option in config.inc.php";
        exit;
      }
    elseif(($phpv == '4')OR($phpv == '5'))
      {
        $swift_folder = "swift_php" . $phpv;
        require("../incs/$swift_folder/EasySwift.php");
      }


  function checkAuthentication($accesslvl)
    {

      if(($accesslvl == 'admin')OR($accesslvl == 'superadmin'))
        {
          $session_valid = '1';
        }

      else
        {
          $session_valid = '0';
        }

      // Return the results
      return $session_valid;

    }

  function checkBanList($ip)
    {

      require('../incs/config.inc.php');
      require('../incs/db.inc.php');

      $query_banlist = "SELECT * from orbitfaq_banlist WHERE `ban_type` = 'ip' AND `detail` = '$ip'";
      $result_banlist = $faqsql_query ($query_banlist)OR DIE( "$sql_query_error $query_banlist");
      $count_banlist = $faqsql_count_rows($result_banlist);

      if($count_banlist != '0')
        {
          $ip_valid = '0';
        }
      else
        {
          $ip_valid = '1';
        }
      // Return the results
      return $ip_valid;

    }

  // This function clears the Smarty Compiled Directory
  function clearSmartyCompiled($path, $ClearSmartyCompiled)
    {
      if($ClearSmartyCompiled == '1')
        {
          $dh = opendir($path);
          $i=1;
          while (($file = readdir($dh)) !== false) {
              if($file != "." && $file != "..") {
                unlink("$path/$file");
              }
          }
        }
    }


  // This function shortens a string
  function ShortenText($text, $chars)
    {

      $text = $text." ";
      $text = substr($text,0,$chars);
      $text = substr($text,0,strrpos($text,' '));

      if($chars != "0")
        {
          $text = $text."...<br /><div id=\"right\" class=\"right mainmenu\">&raquo; read full answer</div>";
        }
        return $text;
    }

  // Function to Check the validity of Email addresses
  function checkEmail($email)
  {

     // Create the syntactical validation regular expression
     $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

     // Presume that the email is invalid
     $valid = 0;

     // Validate the syntax
     if (eregi($regexp, $email))
      {

        // Check if the getmxrr function is available
        if (function_exists('getmxrr'))
          {

            // Split the email address with its domain
            list($username,$domaintld) = split("@",$email);

            // Validate the domain
            if (getmxrr($domaintld,$mxrecords))
              {
                $valid = 1;
              }
            else
              {
                $valid = 0;
              }

          }
        else
          {

           // Because the function is not available trust our
           // Primary Validation on user@domain.com

            $valid = 1;

          }

      }

    return $valid;

  }

  // Random Password Generator
  function makeRandomPassword() {
    $salt = "abchefghjkmnpqrstuvwxyz0123456789";
    srand((double)microtime()*1000000);
      $i = 0;
      while ($i <= 7) {
          $num = rand() % 33;
          $tmp = substr($salt, $num, 1);
          $pass = $pass . $tmp;
          $i++;
      }
      return $pass;
  }



?>